package com.aitaoxue.palx.im.message;

import io.rong.voiplib.NativeObject.AcceptVoIPCallback;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import cn.jpush.android.api.JPushInterface;

import com.aitaoxue.palx.AppContext;
import com.aitaoxue.palx.R;
import com.aitaoxue.palx.db.course.CourseContext;
import com.aitaoxue.palx.db.course.CourseContext.Type;
import com.aitaoxue.palx.util.IntentUtil;
import com.aitaoxue.palx.util.TDevice;

public class PushReceiver extends BroadcastReceiver {
	private static final String TAG = "JPush";

	/**
	 * message作为推送的内容,extras作为内部逻辑的判断依据 需要在10秒内完成,否则anr
	 */
	@SuppressLint("NewApi")
	@Override
	public void onReceive(Context context, Intent intent) {

		Bundle bundle = intent.getExtras();

		if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent
				.getAction())) { // 通知
			//String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
			String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
			JSONObject json=null;
			String type = null;
			try {
				json = new JSONObject(extras);
				type = json.getString("status");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			
			switch (type) {
			case IntentUtil.NEW_ORDER:
				// 向数据库中插入一条type=6的记录
				CourseContext.getInstance().saveType(6, Type.ADD);

				// 发送广播,改变订单红点
				Intent broadIntent = new Intent(IntentUtil.PUSH_NEW_ORDER);
				broadIntent.putExtra("type", "6");
				broadIntent.putExtra("fromreceiver", true);
				context.sendBroadcast(broadIntent);
				break;
			case IntentUtil.ADD_BLOG:
				//提醒添加日志,不需要其他动作
				break;
			case IntentUtil.ACCEPT_COURSE:
				try {
					String finish=json.getString("end");
					
					if(finish.equals("true")){
						//订单状态从1-->2
						CourseContext.getInstance().saveType(1, Type.DELETE);
						CourseContext.getInstance().saveType(2, Type.ADD);
						
						String orderid=json.getString("order_id");
						CourseContext.getInstance().deleteEntity(orderid);
						
						//发送广播,改变红点及刷新页面
						Intent broadIntent2=new Intent(IntentUtil.PUSH_NEW_ORDER);
						broadIntent2.putExtra("type", "2");
						context.sendBroadcast(broadIntent2);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
				break;
			default:
				break;
			}
		} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent
				.getAction())) { // 自定义消息
			

			// 根据应用是否在前台及通知内容决定是否产生通知栏的通知
//			if (TDevice.isShowing(AppContext.getInstance())) {
//			} else {
//				Intent jumpIntent = null;
//				// 在后台,发送通知
//				switch (type) {
//				case IntentUtil.NEW_ORDER:
//					jumpIntent = new Intent(Intent.ACTION_VIEW);
//					Uri uri = Uri
//							.parse(IntentUtil.SCHEMA + IntentUtil.HOST)
//							.buildUpon()
//							.appendPath(
//									IntentUtil.PATH_PREFIX + "/"
//											+ IntentUtil.NEW_ORDER).build();
//					jumpIntent.setData(uri);
//					break;
//
//				default:
//					break;
//				}
//
//				Notification notification = null;
//				PendingIntent pendingIntent = PendingIntent.getActivity(
//						AppContext.getInstance(), 0, jumpIntent,
//						PendingIntent.FLAG_UPDATE_CURRENT);
//
//				if (android.os.Build.VERSION.SDK_INT < 11) {
//					notification = new Notification(AppContext.getInstance()
//							.getApplicationInfo().icon, "通知",
//							System.currentTimeMillis());
//					notification.setLatestEventInfo(AppContext.getInstance(),
//							"爱淘学家教版", message, pendingIntent);
//					notification.flags = Notification.FLAG_AUTO_CANCEL;
//					notification.defaults = Notification.DEFAULT_SOUND;
//				} else {
//					notification = new Notification.Builder(
//							AppContext.getInstance())
//							.setSmallIcon(R.drawable.ic_launcher)
//							.setTicker("通知").setContentTitle("爱淘学家教版")
//							.setContentText(message)
//							.setContentIntent(pendingIntent)
//							.setAutoCancel(true)
//							.setDefaults(Notification.DEFAULT_ALL).build();
//				}
//
//				NotificationManager nm = (NotificationManager) AppContext
//						.getInstance().getSystemService(
//								AppContext.getInstance().NOTIFICATION_SERVICE);
//				nm.notify(0, notification);
//			}

			

		} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent
				.getAction())) { // 点击通知
			
		}
	}
}
